/*
 * Copyright 2019-2029 geekidea(https://github.com/geekidea)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.app.brilliant.shiro.util;

import com.app.brilliant.constant.CommonRedisKey;
import com.app.brilliant.shiro.vo.app.LoginAppUserRedisVo;
import com.app.brilliant.shiro.vo.dev.LoginDevUserRedisVo;
import com.app.brilliant.shiro.vo.sys.LoginSysUserRedisVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


/**
 * 获取登陆信息工具类
 *
 * @author geekidea
 * @date 2018-11-08
 */
@Slf4j
@Component
public class LoginUtil {

    private static RedisTemplate redisTemplate;

    public LoginUtil(RedisTemplate redisTemplate) {
        LoginUtil.redisTemplate = redisTemplate;
    }



    public static LoginSysUserRedisVo getLoginSysUserRedisVoInfo() {
        // 获取当前登陆用户
        String token = JwtTokenUtil.getToken();
        String username = JwtUtil.getUsername(token);
        if (StringUtils.isBlank(username)) {
            return null;
        }
        return (LoginSysUserRedisVo) redisTemplate.opsForValue().get(String.format(CommonRedisKey.LOGIN_SYS_USER, username));
    }


    public static Long getLoginSysUserRedisVoId() {
        LoginSysUserRedisVo loginDevUserRedisVo = getLoginSysUserRedisVoInfo();
        if (loginDevUserRedisVo == null) {
            return null;
        }
        return loginDevUserRedisVo.getId();
    }



    public static LoginDevUserRedisVo getLoginDevUserRedisVoInfo() {
        // 获取当前登陆用户
        String token = JwtTokenUtil.getToken();
        String username = JwtUtil.getUsername(token);
        if (StringUtils.isBlank(username)) {
            return null;
        }
        return (LoginDevUserRedisVo) redisTemplate.opsForValue().get(String.format(CommonRedisKey.LOGIN_DEV_USER, username));
    }


    public static Long getLoginDevUserRedisVoId() {
        LoginDevUserRedisVo loginDevUserRedisVoInfo = getLoginDevUserRedisVoInfo();
        if (loginDevUserRedisVoInfo == null) {
            return null;
        }
        return loginDevUserRedisVoInfo.getId();
    }

    public static LoginAppUserRedisVo getLoginAppUserRedisVoInfo() {
        // 获取当前登陆用户
        String token = JwtTokenUtil.getToken();
        String username = JwtUtil.getUsername(token);
        if (StringUtils.isBlank(username)) {
            return null;
        }
        return (LoginAppUserRedisVo) redisTemplate.opsForValue().get(String.format(CommonRedisKey.LOGIN_APP_USER, username));
    }


    public static Long getLoginAppUserRedisVoId() {
        LoginAppUserRedisVo loginDevUserRedisVoInfo = getLoginAppUserRedisVoInfo();
        if (loginDevUserRedisVoInfo == null) {
            return null;
        }
        return loginDevUserRedisVoInfo.getId().longValue();
    }
}
